/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.Labels;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.elementMapper.Mapper;


public class Example extends AbstractComponent {
	

	public static final String CSSIDFIELD = "divexample";
	public static final String CSSIDCONTENT = "divexamplecontent";
	public static final String NAMEFORMELEMENT = "exampleEntry";

	public static String getEmptyFieldContainer() {

		String ret = AbstractComponent.getEmptyFieldContainer( CSSIDFIELD );
		
		return ret;
	}
	
	public static String getLinkToShowSecondaryFieldInEditMode( boolean shownavigationInEditField ) {
				
		return getLinkToShowSecondaryFieldInEditMode(CSSIDFIELD, shownavigationInEditField, Labels.EXAMPLE.getNamePlural() );
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( model.Labels.EXAMPLE, false, false );
		
		String content = getFormInViewMode( uri, showNavigation, showNavigationInEditMode );
		
		ret += getFormContainer( content, CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}

	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";
					
		
		ret += getContentInEditMode(uri, entry, exceptions, showNavigation);

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	

	public static String getContentInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";
					
		ret += getTitleContainer( model.Labels.EXAMPLE, false, false );
		ret += getFormContainer( getFormInEditMode( uri, entry, exceptions, showNavigation ), CSSIDCONTENT );
		
		return ret;
	}
	
	
	
	
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		
		String ret = "";

		String content = "";

		String entry = Mapper.selectExample( uri );
		if( entry==null ) {
			content=NOTHINGSELECTED;
		}
		else {
			content = getHTMLContent( entry );
		}

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation,
				showNavigationInEditMode );
				
		
		return ret;
	}
		

	private static String getHTMLContent( String entry ) {

		String ret = "<div style=\"margin-right:16px;\">" + StringEscapeUtils.escapeJavaScript( StringEscapeUtils.escapeHtml( entry ).replace("\n", "<br />").replace("\r", "") ) + "</div>";
		
		return ret;
	}

	public static String getFormInEditMode( URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null ) {
			
			String tmp = Mapper.selectExample( uri );
			if( tmp!=null ) {
				actualVersionContent = getHTMLContent( tmp );
			}
		}

		int rows = 2;
		int cols = 60;		
		if(entry==null)
			entry="";
		else {
			rows = entry.split("\n").length;
			
			rows += entry.length() / cols;
			
			rows++;
			
			if( rows < 2 )
				rows = 2;
		}
		
		String content = "<textarea tabindex=\"\" id=\""+NAMEFORMELEMENT+"\" name=\""+NAMEFORMELEMENT+"\" rows=\"" + rows + "\" cols=\"" + cols + "\">" + entry + "</textarea>";

		content +=
			"<div class=\"inputhint\">Free text; do not use any hypertext like HTML!</div>";
		
		ret = renderEditForm(
				Labels.EXAMPLE,  
				content,
				exceptions, 
				actualVersionContent,
				actualTimestamp,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );
		
		return ret;
	}
	

}
